<template>
  <div class="category-list">
    <ul>
      <li v-for="(category, index) in categories" :key="category.id"
          :class="{ active: activeIndex === index }" @click="selectCategory(index)">
        {{ category.name }}
      </li>
    </ul>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        activeIndex: 0,
        categories: [
          { id: 1, name: '台式电脑' },
          { id: 2, name: '笔记本电脑' },
          { id: 3, name: '平板' },
        ],
      }
    },
    methods: {
      selectCategory(index) {
        this.activeIndex = index
        this.$emit('category-change', this.categories[index].id)
      },
    },
  }
</script>

<style scoped>
  .category-list {
    width: 30%;
    background-color: #f4f4f4;
    overflow-y: auto;
  }
  .category-list ul {
    list-style: none;
    padding: 0;
    margin: 0;
  }
  .category-list li {
    padding: 15px;
    cursor: pointer;
  }
  .category-list li.active {
    background-color: #dcdcdc;
  }
</style>
